
const path = require('path')
const fs = require('fs').promises

function static(staticPath) {
    return async (ctx, next) => {
        let url = path.join(staticPath, ctx.path)
        try {
            let statObj = await fs.stat(url)
            if (statObj.isDirectory()) {
                url = path.join(url, 'index.html')
            }
            console.log(url)
            ctx.body = await fs.readFile(url, 'utf-8')
        } catch (error) {
            return next()
        }
    }

}

module.exports = static